<template>
  <div class="pages warehouse">
    <div class="pageLevel" v-if="!sub">
      <!-- 操作框 -->
      <div class="operationBox tableSearch">
        <!-- <el-button type="primary" @click="goBack()" size="mini">返回</el-button> -->
        <el-button type="primary" v-if="setRole('新增')" :disabled="dis" @click="routerPushAdd(0)" size="mini">新增</el-button>
        <el-button type="primary" v-if="setRole('删除')" :disabled="dis" @click="deleteRows()" size="mini">删除</el-button>
      </div>
      <!-- 搜索框 -->
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="" label-width="15px">
            <el-input class="input" :disabled="dis" v-model="selectdata.nameCode" size="small" placeholder="输入仓库编号或名称"></el-input>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelect" :disabled="dis" class="Button">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableMain">
        <el-table ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange" stripe style="width: 100%">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column type="index" width="55"></el-table-column>
          <el-table-column prop="code" label="仓库编号" align="center">
          </el-table-column>
          <el-table-column prop="repositoryName" align="center" label="仓库名称"> </el-table-column>
          <el-table-column prop="repositoryAddress" label="仓库地址" align="center"></el-table-column>
          <el-table-column align="status" label="是否启用" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.status==1?'否':'是'}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" @click="routerPushAdd(1,scope.row.id,scope.row.status)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码 -->
        <div class="pageblock">
          <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10,20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalItems">
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="pageLevel" v-else>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import util from "@/util";
import { getRepositoryListPage, deleteRepository } from "@/api";
export default {
  computed: mapState(["merchantId", "sub", "power"]),
  components: {
    // breadcrumb
  },
  data() {
    return {
      repositoryId: "",
      id: "",
      checked: true,
      classTypeId: "",
      classTypeStatus: "",
      dis: false,
      selectdata: {
        nameCode: ""
      },
      tableData: [],
      multipleSelection: [],
      // 分页开始
      pageSize: 5,
      totalItems: 0,
      currentPage: 1
    };
  },
  watch: {
    sub: {
      handler: function(val, oldVal) {
        if (oldVal != null && val == false) {
          this.getReposListPage(this.pageSize, 1);
        }
      },
      deep: true
    }
  },
  mounted() {
    // 获取表格数据
    this.getReposListPage(this.pageSize, 1);
  },

  methods: {
    //配置权限
    setRole(key) {
      return util.selectTitle(this.power, key);
    },
    routerPushAdd(type, id = "", status) {
      util.routerTo(
        "/order-center/stock-management/stock-warehouse/stock-warehouseView",
        {
          type: type,
          id: id,
          status: status
        }
      );
    },

    // 点击返回按钮
    goBack() {
      history.back();
    },

    // 获取仓库列表
    getReposListPage(pageSize, pageNum) {
      let nameCode = this.selectdata.nameCode;
      let data = {
        pageNum: pageNum,
        pageSize: pageSize,
        shopId: this.merchantId,
        nameCode: nameCode
      };
      getRepositoryListPage(data).then(res => {
        this.totalItems = res.data.pageTotal;
        this.tableData = res.data.list;
      });
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //批量删除
    deleteRows() {
      if (this.multipleSelection.length > 0) {
        this.$confirm("确定要删除吗?", "提示", {
          center: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          closeOnClickModal: false,
          closeOnPressEscape: false
        }).then(() => {
          let arr = [];
          this.multipleSelection.forEach(i => {
            arr.push(i.id);
          });
          let data = {
            repositoryId: arr.toString()
          };
          deleteRepository(data).then(res => {
           if(res.code=="200"){
              this.$message({
              type: "success",
              message: res.msg
            });
            this.getReposListPage(this.pageSize, 1);
           }
          });
        });
      } else {
        this.$message({
          type: "error",
          message: "请选择要删除的商品分类"
        });
      }
    },
    // 查询
    searchSelect() {
      // this.getReposListPage(this.pageSize, this.currentPage);
      this.currentPage=1;
      this.getReposListPage(this.pageSize, this.currentPage);
    },
    //跳页
    toPage(val) {
      this.currentPage = 1;
      this.getReposListPage(this.pageSize, this.currentPage);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage=1;
      this.getReposListPage(val, this.currentPage);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getReposListPage(this.pageSize, val);
    }
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
</style>

